package com.rongshu.widget.calendar;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;
import com.rongshu.R;
import com.utils.DensityUtil;
import com.utils.LogUtil;

public class BanyanCalendarView extends MonthView {
    //签到
    public static String SIGN = "sign";
    //选中日期不可补签
    public static String SELECTED = "selected_cant_sign";
    //选中日期可以补签
    public static String SELECTED_SIGN = "selected_can_sign";

//    private int mRadius;
    /**
     * 标记签到小圆点
     */
    private Paint mPointPaint = new Paint();
    private float mPointRadius;
    private int mPadding;

    /**
     * 补签标记
     */
    private Paint mSupplePointPaint = new Paint();
    private Paint mSuppleTextPaint = new Paint();
    private float mSupplePointRadius;

    /**
     * 今天标记
     */
    private Paint todyPointPaint = new Paint();
    private Paint todyTextPaint = new Paint();
    private float todyPointRadius;

    //补签背景色
    @ColorInt
    private int suppleSignBg_fce4b0;
    @ColorInt
    private int suppleSignBg_f5f5f5;
    //补签字体色
    @ColorInt
    private int suppleTextColor_f99704;
    @ColorInt
    private int suppleTextColor_999999;


    public BanyanCalendarView(Context context) {
        super(context);
        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        //4.0以上硬件加速会导致无效
        mSelectedPaint.setMaskFilter(new BlurMaskFilter(25, BlurMaskFilter.Blur.SOLID));
        //选中的日期效果
//        mRadius = dipToPx(context, 14);
        mSchemePaint.setStyle(Paint.Style.FILL);
        //默认日期字体
        mCurMonthTextPaint.setAntiAlias(true);
        mCurMonthTextPaint.setTextAlign(Paint.Align.CENTER);
        mCurMonthTextPaint.setColor(0xff999999);
        mCurMonthTextPaint.setFakeBoldText(true);
        mCurMonthTextPaint.setTextSize(DensityUtil.dip2px(context, 12));
        //底部签到小圆点
        mPointRadius = DensityUtil.dip2px(context, 2);
        mPadding = DensityUtil.dip2px(getContext(), 3);
        mPointPaint.setAntiAlias(true);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setTextAlign(Paint.Align.CENTER);
        mPointPaint.setColor(ContextCompat.getColor(context, R.color.color_3cc051));
        //补签标记背景
        suppleSignBg_fce4b0 = context.getResources().getColor(R.color.color_fce4b0);
        suppleSignBg_f5f5f5 = context.getResources().getColor(R.color.color_f5f5f5);
        suppleTextColor_f99704 = context.getResources().getColor(R.color.color_f99704);
        suppleTextColor_999999 = context.getResources().getColor(R.color.color_999999);
        mSupplePointRadius = DensityUtil.dip2px(context, 13);
        mSupplePointPaint.setAntiAlias(true);
        mSupplePointPaint.setStyle(Paint.Style.FILL);
        mSupplePointPaint.setTextAlign(Paint.Align.CENTER);
        //补签标记字体
        mSuppleTextPaint.setAntiAlias(true);
        mSuppleTextPaint.setStyle(Paint.Style.FILL);
        mSuppleTextPaint.setTextAlign(Paint.Align.CENTER);
        mSuppleTextPaint.setTextSize(DensityUtil.dip2px(context, 13));
        //今天标记背景
        int todaySignBg = context.getResources().getColor(R.color.color_3cc051);
        int todayTextColor = context.getResources().getColor(R.color.white);
        todyPointRadius = DensityUtil.dip2px(context, 13);
        todyPointPaint.setAntiAlias(true);
        todyPointPaint.setStyle(Paint.Style.FILL);
        todyPointPaint.setTextAlign(Paint.Align.CENTER);
        todyPointPaint.setColor(todaySignBg);
        //今天标记字体
        todyTextPaint.setAntiAlias(true);
        todyTextPaint.setStyle(Paint.Style.FILL);
        todyTextPaint.setTextAlign(Paint.Align.CENTER);
        todyTextPaint.setColor(todayTextColor);
        todyTextPaint.setTextSize(DensityUtil.dip2px(context, 13));
    }

    @Override
    protected void onLoopStart(int x, int y) {

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
//        int cx = x + mItemWidth / 2;
//        int cy = y + mItemHeight / 2;
//        canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        return true;
    }

    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        String scheme = calendar.getScheme();
        int day = calendar.getDay();
        java.util.Calendar instance = java.util.Calendar.getInstance();
        int i = instance.get(java.util.Calendar.DAY_OF_MONTH);
        int heightHalf = mItemHeight / 2;
        int widthHalf = mItemWidth / 2;
        if (day == i) { //今天标记
            mPadding = DensityUtil.dip2px(getContext(), 3);
            canvas.drawCircle(x + widthHalf, y + heightHalf, todyPointRadius, todyPointPaint);
            canvas.drawText("今", x + widthHalf, y + mItemHeight / 2 + 2 * mPadding,
                    todyTextPaint);
        }
        if (!TextUtils.isEmpty(scheme) && TextUtils.equals(scheme, SELECTED_SIGN)) { //该日期可补签
            mSupplePointPaint.setColor(suppleSignBg_fce4b0);
            mSuppleTextPaint.setColor(suppleTextColor_f99704);
            mPadding = DensityUtil.dip2px(getContext(), 3);
            canvas.drawCircle(x + widthHalf, y + heightHalf, mSupplePointRadius, mSupplePointPaint);
            canvas.drawText("补", x + widthHalf, y + mItemHeight / 2 + 2 * mPadding,
                    mSuppleTextPaint);
        } else if (!TextUtils.isEmpty(scheme) && TextUtils.equals(scheme, SELECTED)) {//该日期不可补签
            mSupplePointPaint.setColor(suppleSignBg_f5f5f5);
            mSuppleTextPaint.setColor(suppleTextColor_999999);
            mPadding = DensityUtil.dip2px(getContext(), 3);
            canvas.drawCircle(x + widthHalf, y + heightHalf, mSupplePointRadius, mSupplePointPaint);
            canvas.drawText("补", x + widthHalf, y + mItemHeight / 2 + 2 * mPadding,
                    mSuppleTextPaint);
        } else { //底部小圆点标记
            mPadding = DensityUtil.dip2px(getContext(), 1);
            canvas.drawCircle(x + widthHalf, y + mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
        }
        LogUtil.d("更新日历状态" + calendar.getYear() + "年" + calendar.getMonth() + "月" + calendar.getDay() + "日");
    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
        String scheme = calendar.getScheme();
        int day = calendar.getDay();
        java.util.Calendar instance = java.util.Calendar.getInstance();
        int i = instance.get(java.util.Calendar.DAY_OF_MONTH);
        if (!TextUtils.isEmpty(scheme) && TextUtils.equals(scheme, SELECTED)) {
            canvas.drawText("", cx, baselineY, mSelectTextPaint);
        } else if (!TextUtils.isEmpty(scheme) && TextUtils.equals(scheme, SELECTED_SIGN)) {
            canvas.drawText("", cx, baselineY, mSelectTextPaint);
        } else if (day == i) {
            canvas.drawText("", cx, baselineY, mSelectTextPaint);
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    mSelectTextPaint);
        }
    }
}
